//生成菜单
var menuItem = Vue.extend({
	name: 'menu-item',
	props:{item:{},url:''},
	template:[
	          '<li :class="item.url==url?\'active\':\'\'">',
	          '<a v-if="item.type === \'FOLDER\'" href="javascript:;">',
	          '<i v-if="item.icon != null" :class="item.icon"></i>',
	          '<span>{{item.name}}</span>',
	          '<i class="fa fa-angle-left pull-right"></i>',
	          '</a>',
	          '<ul v-if="item.type === \'FOLDER\'" class="treeview-menu menu-open" style="display:block">',
	          '<menu-item :item="item" :url="url" v-for="item in item.list"></menu-item>',
	          '</ul>',
	          '<a v-if="item.type === \'MENU\'" :href="item.url"><i v-if="item.icon != null" :class="item.icon"></i><i v-else class="fa fa-circle-o"></i> {{item.name}}</a>',
	          '</li>'
	].join('')
});

//iframe自适应
// $(window).on('resize', function() {
// 	var $content = $('.content');
// 	$content.height($(this).height() - 120);
// 	$content.find('iframe').each(function() {
// 		$(this).height($content.height()-20);
// 	});
// }).resize();

//注册菜单组件
Vue.component('menuItem',menuItem);
var mixin = {
    data:{
        user:{},
        menuList:{},
        main:"",
        password:'',
        newPassword:'',
        navTitle:"控制台"
    },
    methods: {
        getMenuList: function (event) {
            $.getJSON("/admin/currentUserMenu?_"+$.now(), function(r){
                vm.menuList = r.menuList;
            });
        },
        getUser: function(){
            $.getJSON("/admin/currentUser?_"+$.now(), function(r){
                vm.user = r.user;
            });
        },
        updatePassword: function(){
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "修改密码",
                area: ['550px', '270px'],
                shadeClose: false,
                content: jQuery("#passwordLayer"),
                btn: ['修改','取消'],
                btn1: function (index) {
                    var data = "password="+vm.password+"&newPassword="+vm.newPassword;
                    $.ajax({
                        type: "POST",
                        url: "/tts/sys/user/password",
                        data: data,
                        contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                        success: function(result){
                            if(result.code == 0){
                                layer.close(index);
                                layer.alert('修改成功', function(index){
                                    location.reload();
                                });
                            }else{
                                layer.alert(result.msg);
                            }
                        }
                    });
                }
            });
        }
    },
    computed:{
        url :function () {
            return window.location.pathname;
        }
    },
    created: function(){
        this.getMenuList();
        this.getUser();
    },
    updated: function(){
        //路由
        var router = new Router();
        routerList(router, vm.menuList);
        router.start();
    }
};
// $('.container').perfectScrollbar();

function routerList(router, menuList){
	for(var key in menuList){
		var menu = menuList[key];
		if(menu.type == "FOLDER"){
			routerList(router, menu.list);
		}else if(menu.type == "MENU"){
			router.add('#'+menu.url, function() {
				var url = window.location.hash;
				
				//替换iframe的url
			    vm.main = url.replace('#', '');
			    
			    //导航菜单展开
			    $(".treeview-menu li").removeClass("active");
			    $("a[href='"+url+"']").parents("li").addClass("active");
			    
			    vm.navTitle = $("a[href='"+url+"']").text();
			});
		}
	}
}
